package com.ctlele.team.space.activity.impl;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.ctlele.team.space.R;
import com.ctlele.team.space.activity.BaseActivity;
import com.ctlele.team.space.adapter.ChatMsgItemAdapter;
import com.ctlele.team.space.entity.vo.ChatMessageVo;
import com.ctlele.team.space.util.MessageUtils;

import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class ChatWithActivity extends BaseActivity {

    private static final String TAG = "ChatWithActivity";

    /**
     * 消息输入框
     */
    private EditText mMsgInput;

    /**
     * 点击发送
     */
    private ImageView mSend;

    /**
     * 聊天窗口的标题
     */
    private TextView mTitle;

    private ImageView mBack;

    private RecyclerView mContent;

    private ChatMsgItemAdapter mAdapter;

    private String mMe;

    private String mTo;

    private String mToAvatar;

    private String mMeAvatar;

    public static final String ME_KEY = "me";

    public static final String TO_KEY = "to";

    public static final String TO_AVATAR_KEY = "to_avatar";

    public static final String ME_AVATAR_KEY = "me_avatar";

    Timer timer = new Timer();

    TimerTask readMsgTask = new TimerTask() {
        @Override
        public void run() {
            List<ChatMessageVo> newMessage =
                    MessageUtils.interceptList(mTo);
            if (!(newMessage == null || newMessage.isEmpty())) {
                for (ChatMessageVo message : newMessage) {
                    if (message.getFrom().equals(mMe)) {
                        message.setFromAvatar(mMeAvatar);
                    } else {
                        message.setFromAvatar(mToAvatar);
                    }
                }

                // 刷新消息列表
                mAdapter.appendData(newMessage);
            }
        }
    };

    @Override
    protected int initLayout() {
        return R.layout.activity_chat_with;
    }

    @Override
    protected void initView() {
        mMsgInput = findViewById(R.id.message_input);
        mSend = findViewById(R.id.send_to);
        mContent = findViewById(R.id.chat_content);
        View header = findViewById(R.id.page_header);
        mTitle = header.findViewById(R.id.title);
        mBack = header.findViewById(R.id.go_back);
        mAdapter = new ChatMsgItemAdapter(this, mContent);
    }

    @Override
    protected void initData() {
        mMe = getIntent().getStringExtra(ME_KEY);
        mTo = getIntent().getStringExtra(TO_KEY);
        mMeAvatar = getIntent().getStringExtra(ME_AVATAR_KEY);
        mToAvatar = getIntent().getStringExtra(TO_AVATAR_KEY);

        mAdapter.setMe(mMe);
        mAdapter.setFriend(mTo);

        mBack.setOnClickListener(view -> onBackPressed());
        mTitle.setText(mTo);

        mContent.setAdapter(mAdapter);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mContent.setLayoutManager(linearLayoutManager);

        timer.scheduleAtFixedRate(readMsgTask, 100, 2500);

        mSend.setOnClickListener(view -> {
            String inputMsg = mMsgInput.getText().toString();
            if (TextUtils.isEmpty(inputMsg)) {
                return;
            }
            Date now = new Date();
            ChatMessageVo message = new ChatMessageVo();
            message.setFrom(mMe);
            message.setTo(mTo);
            message.setMsg(inputMsg);
            message.setFromAvatar(mMeAvatar);
            message.setGmtCreate(now);
            message.setGmtModify(now);
            mAdapter.appendData(message);

            String json = JSON.toJSONString(message);
            MessageUtils.push(json);

            // 清空输入框
            mMsgInput.setText("");
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
        readMsgTask.cancel();
        MessageUtils.cache(mTo, mAdapter.getMessageList());
        // MessageUtils.sort(mTo);
    }
}